<?php

namespace app\middleware;

use app\Request;
use Closure;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class TeacherAuthMiddleware
{
    // 处理请求
    public function handle($request, Closure $next){
        // 获取用户
        $user = $request->user ?? null;

        // 如果用户不存在或者用户角色不是教师，则返回403错误
        if (!$user || ($user['role'] ?? '') !== 'teacher') {
            return json(['code' => 403, 'msg' => '无教师权限'], 403);
        }

        // 将用户信息存入请求中
        $request->admin = $user;

        // 继续处理请求
        return $next($request);
    }

}
